package com.example.adminserivce.Service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.adminserivce.Mapper.TeacherMapper;
import com.example.adminserivce.Service.TeacherService;
import com.example.common.constants.RdsConstants;
import com.example.common.domain.dto.search.TeacherSearchDto;
import com.example.common.domain.pojo.Teacher;
import com.example.common.utils.CacheClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


// 教师服务实现
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired
    private CacheClient cacheClient;
    @Autowired
    private TeacherMapper teacherMapper;

    public Page<Teacher> pageByCriteria(Page<Teacher> page, TeacherSearchDto params) {
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
        if (params.getName() != null && !params.getName().isEmpty()) {
            wrapper.like(Teacher::getName, params.getName());
        }
        if (params.getMinAge() != null) {
            wrapper.ge(Teacher::getAge, params.getMinAge());  //大于等于
        }
        if (params.getMaxAge() != null) {
            wrapper.le(Teacher::getAge, params.getMaxAge());  //小于等于
        }
        if (params.getGender() != null && !params.getGender().isEmpty()) {  //等于
            wrapper.eq(Teacher::getGender, params.getGender());
        }
//        if (params.getMajor() != null && !params.getMajor().isEmpty()) {  //模糊查询
//            wrapper.eq(Teacher::getMajor, params.getMajor());
//        }
        return this.page(page, wrapper);
    }

    @Override
    public Teacher getTeacherById(String id) {
        // 使用缓存穿透策略获取教师信息
        return cacheClient.queryWithPassThrough(
                RdsConstants.TEACHER_INFO,
                id,
                Teacher.class,
                this::getById, // 使用MyBatis-Plus的基础方法
                RdsConstants.CACHE_NULL_TTL,
                TimeUnit.MINUTES,
                2,
                TimeUnit.MINUTES
        );
    }

    @Override
    public Teacher getTeacherByIdWithLogicalExpire(String id) {
        // 使用逻辑过期策略获取教师信息
        return cacheClient.queryWithLogicalExpire(
                RdsConstants.TEACHER_INFO,
                id,
                Teacher.class,
                this::getById, // 使用MyBatis-Plus的基础方法
                RdsConstants.CACHE_NULL_TTL,
                TimeUnit.MINUTES,
                false
        );
    }

    @Override
    public Teacher saveTeacher(Teacher teacher) {
        // 保存教师信息到数据库
        boolean success = save(teacher); // 使用MyBatis-Plus的基础方法

        if (success) {
            // 更新缓存（使用逻辑过期）
            cacheClient.setWithLogicalExpire(
                    RdsConstants.TEACHER_INFO + teacher.getId(),
                    teacher,
                    RdsConstants.CACHE_NULL_TTL,
                    TimeUnit.MINUTES
            );
            return teacher;
        }

        return null;
    }

    @Override
    public Teacher updateTeacher(Teacher teacher) {
        // 更新数据库中的教师信息
        boolean success = updateById(teacher); // 使用MyBatis-Plus的基础方法

        if (success) {
            // 更新缓存（使用逻辑过期）
            cacheClient.setWithLogicalExpire(
                    RdsConstants.TEACHER_INFO + teacher.getId(),
                    teacher,
                    RdsConstants.CACHE_NULL_TTL,
                    TimeUnit.MINUTES
            );
            return teacher;
        }

        return null;
    }

    @Override
    public void deleteTeacher(String id) {
        // 从数据库删除教师
        removeById(id); // 使用MyBatis-Plus的基础方法

        // 删除缓存
        String cacheKey = RdsConstants.TEACHER_INFO + id;
        cacheClient.delete(cacheKey);
    }
}
